package com.cskaoyan.service;

import com.cskaoyan.bean.configBo.ConfigExpressBo;
import com.cskaoyan.bean.configBo.ConfigMallBo;
import com.cskaoyan.bean.configBo.ConfigOrderBo;
import com.cskaoyan.bean.configBo.ConfigWXBo;
import com.cskaoyan.mapper.SystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    SystemMapper systemMapper;

    @Override
    public Map selectMallConfig(String condition) {
        List<Map> maps = systemMapper.selectMallConfig(condition);
        HashMap<String, String> hashMap = new HashMap<>();
        for (Map map : maps) {
            String key = (String) map.get("key");
            int i = key.indexOf("_");
            key = key.substring(0,i) + key.substring(i+1);
            hashMap.put(key,(String)map.get("value"));
        }
        return hashMap;
    }

    @Override
    public void updateConfigMall(ConfigMallBo configMallBO) {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        systemMapper.updateConfigMall(configMallBO.getCskaoyanmall_mall_address(),
                timestamp,"cskaoyan_mall_mall_address");
        systemMapper.updateConfigMall(configMallBO.getCskaoyanmall_mall_name(),
                timestamp,"cskaoyan_mall_mall_name");
        systemMapper.updateConfigMall(configMallBO.getCskaoyanmall_mall_phone(),
                timestamp,"cskaoyan_mall_mall_phone");
        systemMapper.updateConfigMall(configMallBO.getCskaoyanmall_mall_qq(),
                timestamp,"cskaoyan_mall_mall_qq");
    }

    @Override
    public void updateConfigExpress(ConfigExpressBo configExpressBo) {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        systemMapper.updateConfigExpress(configExpressBo.getCskaoyanmall_express_freight_min(),
                timestamp,"cskaoyan_mall_express_freight_min");
        systemMapper.updateConfigExpress(configExpressBo.getCskaoyanmall_express_freight_value(),
                timestamp,"cskaoyan_mall_express_freight_value");
    }

    @Override
    public void updateConfigOrder(ConfigOrderBo configOrderBo) {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        systemMapper.updateConfigOrder(configOrderBo.getCskaoyanmall_order_comment(),
                timestamp,"cskaoyan_mall_order_comment");
        systemMapper.updateConfigOrder(configOrderBo.getCskaoyanmall_order_unconfirm(),
                timestamp,"cskaoyan_mall_order_unconfirm");
        systemMapper.updateConfigOrder(configOrderBo.getCskaoyanmall_order_unpaid(),
                timestamp,"cskaoyan_mall_order_unpaid");
    }

    @Override
    public void updateConfigWX(ConfigWXBo configWXBo) {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        systemMapper.updateConfigWX(configWXBo.getCskaoyanmall_wx_catlog_goods(),
                timestamp,"cskaoyan_mall_wx_catlog_goods");
        systemMapper.updateConfigWX(configWXBo.getCskaoyanmall_wx_catlog_list(),
                timestamp,"cskaoyan_mall_wx_catlog_list");
        systemMapper.updateConfigWX(configWXBo.getCskaoyanmall_wx_index_brand(),
                timestamp,"cskaoyan_mall_wx_index_brand");
        systemMapper.updateConfigWX(configWXBo.getCskaoyanmall_wx_index_hot(),
                timestamp,"cskaoyan_mall_wx_index_hot");
        systemMapper.updateConfigWX(configWXBo.getCskaoyanmall_wx_index_new(),
                timestamp,"cskaoyan_mall_wx_index_new");
        systemMapper.updateConfigWX(configWXBo.getCskaoyanmall_wx_index_topic(),
                timestamp,"cskaoyan_mall_wx_index_topic");
        systemMapper.updateConfigWX(configWXBo.getCskaoyanmall_wx_share(),
                timestamp, "cskaoyan_mall_wx_share");
    }
}
